<?php
/**
 * RoleController
 * 
 * @author naspeh
 * @version $Id: RoleController.php 661 2008-06-16 10:38:02Z naspeh $
 */
class RoleController extends Core_Controller_Action
{
    /**
     * Выводит список ролей
     *
     */
    public function listAction ()
    {
        $list =  new Core_Role_Record();
        $list->process($this->getRequest());
        $this->view->list = $list;
    }
    /**
     * Редактирует роль
     *
     */
    public function editAction ()
    {
        $request = $this->getRequest();
        $role = $this->getRole();
        $form = new Core_Role_EditForm(array('params' => array('role' => $role)));
        if ($form->isValid($request)) {
            $form->process();
        }
        $this->view->role = $role;
        $this->view->form = $form;
        Core_View_Bar_Navigation::add('role/list', 0);        
    }
    /**
     * Добавляет роль
     *
     */
    public function addAction ()
    {
        $request = $this->getRequest();
        $form = new Core_Role_AddForm();
        if ($form->isValid($request)) {
            $form->process();
        }
        $this->view->form = $form;        
    }
    /**
     * Устанавливает привилегии
     *
     */
    public function accessAction()
    {
        $form = new Core_Role_AccessForm(array('params' => array('role' => $this->getRole())));
        if ($form->isValid($this->getRequest())) {
           $form->process();
        }
        $this->view->form = $form;
        Core_View_Bar_Navigation::add('role/list', 0);
    }
    /**
     * Возвращает роль из url
     *
     * @return Zend_Db_Table_Row
     */
    public function getRole()
    {
        $request = $this->getRequest();
        if (null !== ($role = $request->getParam('role'))) {
            $tableRoles = new Core_Table_Roles();
            $role = $tableRoles->fetchRow(array('name = ?' => $role));
            if (is_null($role)) {
                throw new Core_Exception('wrongRole# ' . $role, 404);
            }
            $acl = Core_Acl::getRegisrty();
            $roleResource = new Core_Acl_Resource_Role($role);
            $acl->active($roleResource);
            if (!$acl->isAllowed(Core_Auth::get('id'), $roleResource)) {
            	throw new Core_Exception('badRole#', 403);
            }
        } else {
            throw new Core_Exception('roleNotFound# ' . $role, 404);
        }
        return $role;
    }    
}